/*=====================================================================*
 *                   Copyright (C) 2011 Paul Mineiro                   *
 * All rights reserved.                                                *
 *                                                                     *
 * Redistribution and use in source and binary forms, with             *
 * or without modification, are permitted provided that the            *
 * following conditions are met:                                       *
 *                                                                     *
 *     * Redistributions of source code must retain the                *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer.                                       *
 *                                                                     *
 *     * Redistributions in binary form must reproduce the             *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer in the documentation and/or            *
 *     other materials provided with the distribution.                 *
 *                                                                     *
 *     * Neither the name of Paul Mineiro nor the names                *
 *     of other contributors may be used to endorse or promote         *
 *     products derived from this software without specific            *
 *     prior written permission.                                       *
 *                                                                     *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND              *
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,         *
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES               *
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE             *
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER               *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                 *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES            *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE           *
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR                *
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF          *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY              *
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE             *
 * POSSIBILITY OF SUCH DAMAGE.                                         *
 *                                                                     *
 * Contact: Paul Mineiro <paul@mineiro.com>                            *
 *=====================================================================*/

#ifndef __DISTRIBUTION_H__
#define __DISTRIBUTION_H__

typedef struct _Distribution Distribution;
struct _Distribution 
{
  float (*logpdf)  (float, const Distribution*);
  float (*dlogpdf) (float, const Distribution*);
  float (*ddlogpdf) (float, const Distribution*);
};

#define distribution_logpdf(p, x)               \
  ({                                            \
     const Distribution* _dlp = (p);            \
     float _dlx = (x);                          \
     _dlp->logpdf (_dlx, _dlp);                 \
  })

#define distribution_dlogpdf(p, x)              \
  ({                                            \
     const Distribution* _ddlp = (p);           \
     float _ddlx = (x);                         \
     _ddlp->dlogpdf (_ddlx, _ddlp);             \
  })

#define distribution_ddlogpdf(p, x)             \
  ({                                            \
     const Distribution* _ddlp = (p);           \
     float _ddlx = (x);                         \
     _ddlp->ddlogpdf (_ddlx, _ddlp);            \
  })

typedef struct _NormalDistribution NormalDistribution;
struct _NormalDistribution 
{
  Distribution  base;

  float        mean;
  float        sq_stddev;
  float        log_stddev;
};

NormalDistribution
normal_distribution (float mean,
                     float stddev);

typedef struct _ParameterizedDistribution ParameterizedDistribution;
struct _ParameterizedDistribution 
{
  float (*logpdf) (float, float, const ParameterizedDistribution*);
  float (*d0logpdf) (float, float, const ParameterizedDistribution*);
  float (*d1logpdf) (float, float, const ParameterizedDistribution*);
};

#define parameterized_distribution_logpdf(p, x, y)      \
  ({                                                    \
     const ParameterizedDistribution* _pdlp = (p);      \
     float _pdlx = (x);                                 \
     float _pdly = (y);                                 \
     _pdlp->logpdf (_pdlx, _pdly, _pdlp);               \
  })

#define parameterized_distribution_d0logpdf(p, x, y)    \
  ({                                                    \
     const ParameterizedDistribution* _pdd0lp = (p);    \
     float _pdd0lx = (x);                               \
     float _pdd0ly = (y);                               \
     _pdd0lp->d0logpdf (_pdd0lx, _pdd0ly, _pdd0lp);     \
  })

#define parameterized_distribution_d1logpdf(p, x, y)    \
  ({                                                    \
     const ParameterizedDistribution* _pdd1lp = (p);    \
     float _pdd1lx = (x);                               \
     float _pdd1ly = (y);                               \
     _pdd1lp->d1logpdf (_pdd1lx, _pdd1ly, _pdd1lp);     \
  })

typedef struct _ParameterizedNormalDistribution ParameterizedNormalDistribution;
struct _ParameterizedNormalDistribution 
{
  ParameterizedDistribution base;

  float sq_stddev;
  float log_stddev;
};

ParameterizedNormalDistribution
parameterized_normal_distribution (float stddev);

#endif /* __DISTRIBUTION_H__ */
